﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrbaCommerce.Calificar_Vendedor
{

    public partial class ListadoCompras: Form
    {
        private int user_id;
        private string strFiltros;   
        private DataGridViewRow filaSeleccionada;
        private bool soloSinCalificar;

        // Page
        private int mintTotalRecords = 0;
        private int mintPageSize = 10;
        private int mintPageCount = 0;
        private int mintCurrentPage = 1;


        private void fillGrid()
        {
            // For Page view.
            //this.mintPageSize = int.Parse(this.bindingNavigator1.Text);
            this.mintTotalRecords = getCount();
            this.mintPageCount = this.mintTotalRecords / this.mintPageSize;

            // Adjust page count if the last page contains partial page.
            if (this.mintTotalRecords % this.mintPageSize > 0)
                this.mintPageCount++;

            this.mintCurrentPage = 0;

            loadPage();
        }

        public int getCount()
        {
            // This select statement is very fast compare to SELECT COUNT(*)

            SqlConnection con = Program.obtenerConexion();
            string strSql = "SELECT com_id as 'Código', com_fecha as Fecha, com_cant as Cantidad, com_publicacion_id as 'Publicación'"
                + " FROM Monkey_Coders.Compras"
                + strFiltros;
                

            SqlCommand cmd = new SqlCommand(strSql, con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "Monkey_Coders.Compras");

            int intCount = ds.Tables[0].Rows.Count;


            cmd.Dispose();


            return intCount;
        }

        private void loadPage()
        {
            string strSql = "";
            int intSkip = 0;

            if (mintPageCount == 0)
                return;

            SqlConnection con = Program.obtenerConexion();
            intSkip = (this.mintCurrentPage * this.mintPageSize);

            // Select only the n records.

            strSql = "SELECT TOP " + this.mintPageSize +
                " com_id as 'Código', com_fecha as Fecha, com_cant as Cantidad, com_publicacion_id as 'Publicación'"
                + " FROM Monkey_Coders.Compras"
                + strFiltros
                + " AND com_id NOT IN " +
                "(SELECT TOP " + intSkip +
                " com_id "
                + " FROM Monkey_Coders.Compras"
                + strFiltros + ")";

            

            SqlCommand cmd = new SqlCommand(strSql, con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            da.Fill(ds, "Monkey_Coders.Compras");

            // Populate Data Grid
            dataGridView1.DataSource = ds.Tables["Monkey_Coders.Compras"].DefaultView;

            // Show Status
            labelPaginas.Text = (this.mintCurrentPage + 1).ToString() +
              " / " + this.mintPageCount.ToString();

            cmd.Dispose();
            da.Dispose();
            ds.Dispose();
        }





        public ListadoCompras(int user_id, bool soloSinCalificar)
        {
            this.user_id = user_id;
            this.soloSinCalificar = soloSinCalificar;
            strFiltros = " WHERE com_usuario_comprador_id = " + user_id;

            if (soloSinCalificar)
                strFiltros = strFiltros
                    + " AND com_id NOT IN (SELECT cal_compra_id FROM Monkey_Coders.Calificaciones)";

            InitializeComponent();

        }

        private void buttonBuscar_Click(object sender, EventArgs e)
        {
            buscar();
        }

        public void buscar()
        {
            dataGridView1.Columns.Clear();

            fillGrid();

            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(btn);
            btn.HeaderText = "Calificar";
            btn.Name = "boton";
            btn.UseColumnTextForButtonValue = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex < 0 || e.ColumnIndex < 0)
                return;


            filaSeleccionada = dataGridView1.Rows[e.RowIndex];

            if (dataGridView1.Columns[e.ColumnIndex].Name == "boton")
            {
                    Form calificar = new FormCalificar(user_id, filaSeleccionada,this);
                    calificar.Show();
            }
        }

        private void buttonLimpiar_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns.Clear();
        }

        private void buttonPrimera_Click(object sender, EventArgs e)
        {

            this.mintCurrentPage = 0;

            loadPage();
        }

        private void buttonAtras_Click(object sender, EventArgs e)
        {
            if (this.mintCurrentPage == this.mintPageCount)
                this.mintCurrentPage = this.mintPageCount - 1;

            this.mintCurrentPage--;

            if (this.mintCurrentPage < 1)
                this.mintCurrentPage = 0;

            loadPage();
        }

        private void buttonAdelante_Click(object sender, EventArgs e)
        {
            this.mintCurrentPage++;

            if (this.mintCurrentPage > (this.mintPageCount - 1))
                this.mintCurrentPage = this.mintPageCount - 1;

            loadPage();
        }

        private void buttonLast_Click(object sender, EventArgs e)
        {
            this.mintCurrentPage = this.mintPageCount - 1;
            loadPage();
        }

        private void ListadoCompras_Load(object sender, EventArgs e)
        {

        }

    }
}
